×
☰ See All Chapters

try with resources

What is resource?

  1. It may be a Database like MySQL, Oracle 

  2. It may be a file. 

  3. It may be a socket connection Etc… 

In java if we make connection to database, we must explicitly close database connection before JVM shutdown. If we open a file we must explicitly close opened file before JVM shutdown and so on. In order close these resources we generally use finally block (till java 6) since finally block always executes irrespective of exceptions and errors.

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. AutoCloseable interface is also introduced in java 1.7

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

class A{

        static String readFirstLineFromFile(String path) throws IOException {

                try (BufferedReader br = new BufferedReader(new FileReader(path))) {

                   return br.readLine();

                 }

        }

}

public class Test {

        public static void main(String[] args) throws IOException {

        A a= new A();

        String s=a.readFirstLineFromFile("I:/A.txt");

        System.out.println(s);

        }

}

Prior JDK7

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

 

class A{

        static String readFirstLineFromFileWithFinallyBlock(String path) throws IOException {

                BufferedReader br = new BufferedReader(new FileReader(path));

                try {

                        return br.readLine();

                } finally {

                        if (br != null) br.close();

                }

        }

}

 

public class Test {

        public static void main(String[] args) throws IOException {

        A a= new A();

        String s=a.readFirstLineFromFileWithFinallyBlock("I:/A.txt");

        System.out.println(s);

        }

}

JDBC Program using try with resource block

package com.manum.hassan;

 

import java.sql.Connection;

import java.sql.Driver;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class MysqlCon {

        public static void main(String[] args) {

                try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "welcome");

                                Statement stmt = con.createStatement();

                                ResultSet rs = stmt.executeQuery("select * from emp");) {

 

                        Driver drive = new com.mysql.jdbc.Driver();

                        DriverManager.registerDriver(drive);

 

                        Class.forName("com.mysql.jdbc.Driver");

 

                        while (rs.next()) {

                                int id = rs.getInt(1);

                                String name = rs.getString(2);

                                System.out.println(id + "\t" + name);

                        }

 

                } catch (ClassNotFoundException e) {

                        System.out.println("Exception caught...." + e);

                        e.printStackTrace();

                } catch (SQLException e) {

                        System.out.println("Exception caught...." + e);

                        e.printStackTrace();

                }

        }

}

 


All Chapters
Author